<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <select class="province">
        <option value="">请选择省份</option>
        <!-- <option value="A001">北京</option>
        <option value="A002">上海</option>
        <option value="A003">湖北</option>
        <option value="A004">湖南</option> -->
    </select>
    <select class="city" name="" id="">
        <option value="">请选择市/区</option>
        <!-- <option value="A001">朝阳</option>
        <option value="A002">海淀</option>
        <option value="A003">沙河</option>
        <option value="A004">上地</option> -->
    </select>
</body>
<script>
    var province = document.getElementsByClassName("province")[0];
    var city = document.getElementsByClassName("city")[0];
    // var body = document.querySelector("body");

    var xhr = new XMLHttpRequest();

    xhr.open("get", "../day44/pro.json", true);
    // console.log(xhr.readyState);

    xhr.send();

    xhr.onreadystatechange = function () {
        // console.log("readyState", xhr.readyState);
        if (xhr.readyState == 4 && xhr.status == 200) {
            // console.log(xhr.responseText);

            var data = JSON.parse(xhr.responseText);
            // console.log(data);
            var { list: proList } = data;
            // console.log(proList);

            html = '<option value="">请选择省份</option>';

            proList.forEach(({ city_id, city_name }) => html += `<option value="${city_id}">${city_name}</option>`);

            province.innerHTML = html;

            province.onchange = function () {
                var cityId = this.value;
                if (cityId) {
                    var thispro = proList.find(({ city_id }) => city_id == cityId);

                    var { list: cityList } = thispro;
                    // console.log(citylist);

                    var html = `<option value="">请选择城市</option>`;

                    cityList.forEach(({ city_id, city_name }) => html += `<option value="${city_id}">${city_name}</option>`);

                    city.innerHTML = html;
                } else {
                    city.innerHTML = `<option value="">请选择城市</option>`;
                }
            }
        }
    }
</script>

</html>